const fs = require('fs');
const path = require("path");
const router = require('koa-router')();
const webapiConfig = require('./webapiConfig');

router.prefix('/users')

// 手机 摇一摇 能力
router.get('/shake', async function (ctx, next) {
  await ctx.render('shake/shake'); 
})

// 世界杯 对战表
router.get('/against', async function (ctx, next) {
  await ctx.render('against/against');
})

// web 唤起拨号
router.get('/phone', async function (ctx, next) {
  await ctx.render('phone/phone');
})

// 滑块验证码
router.get('/sliderValidation', async function (ctx, next) {
  await ctx.render('sliderValidation/sliderValidation');
})

// 各种 图像处理 算法
router.get('/imageHandle', async function (ctx, next) {
  await ctx.render('imageHandle/imageHandle');
})

// 实现一个 promise 类
router.get('/promise', async function (ctx, next) {
  await ctx.render('promise/promise');
})

router.get('/rollingThrough', async function (ctx, next) {
  await ctx.render('rollingThrough/rollingThrough');
})

// 一些视频播放的特性
router.get('/aboutVideo', async function (ctx, next) {
  await ctx.render('aboutVideo/aboutVideo');
})
// 视频播放页面
router.get('/aboutVideo/:src', async function (ctx, next) {
  await ctx.render('aboutVideo/curVideo', {
    path: ctx.params.src
  });
})

// 下载-下载pdf文件
router.get('/download/:ext', async function (ctx, next) {
  let pageUrl = `download/download_`;
  switch (ctx.params.ext) {
    case 'pdf': pageUrl += ctx.params.ext;
    default: break;
  }
  await ctx.render(pageUrl);
})

// Web API 列表
router.get('/webapi', async function (ctx, next) {
  await ctx.render('webapi/webapiList', {
    title: 'Web APIs',
    list: webapiConfig,
    bp: global.basePath
  })
})

// Web API 具体页面
router.get('/webapi/:api/:subLink?', async function (ctx, next) {
  let api = ctx.params.api, param = '', subLink = ctx.params.subLink, MDNOrW3CDoc = '', exampleDoc = '';
  for(let i = 0; i < webapiConfig.length; i += 1) {
    if(api === webapiConfig[i].api) {
      if (webapiConfig[i].subLink) {
        for(let j = 0; j < webapiConfig[i].subLink.length; j += 1) {
          if (webapiConfig[i].subLink[j].api === subLink) {
            param = webapiConfig[i].subLink[j];
            exampleDoc = webapiConfig[i].subLink[j].document;
            break;
          }
        }
      } else {
        param = webapiConfig[i];
      }
      MDNOrW3CDoc = webapiConfig[i].document;
      break;
    }
  }
  let folderName = api;
  let fileName = subLink ? subLink : api;
  await ctx.render(`webapi/${folderName}/${fileName}`, {
    title: param.name,
    api: param,
    exampleDoc,
    MDNOrW3CDoc
  })
})

// forginObject svg元素应用
router.get('/forginObject', async (ctx, next) => {
  await ctx.render('forginObject/forginObject');
})

// 经纬度平面投影算法页面
router.get('/geographicalCoordinateProjection', async ctx => {
  await ctx.render('geographicalCoordinateProjection/geographicalCoordinateProjection');
})

/* --==================================-- */
/* --============== POST ==============-- */
/* --==================================-- */
router.post('/verifySliderCode', async function (ctx, next) {
  ctx.body = '0';
})

router.post('/getInfo', async function (ctx, next) {
  ctx.body = {
    "height": 85,
    "retCode": "0",
    "token": "eca876aebdc30fe299d1394ec0b17312"
  }
})

// 下载-下载pdf
router.post('/getPDFText', async function (ctx, next) {
  let pdfBase64Text = fs.readFileSync(path.resolve(__dirname, '../data/pdf.txt'));
  ctx.body = pdfBase64Text;
})

// Beacon 接口
router.post('/webpai/beaconpost', async function (ctx, next) {
  ctx.body = {};
})

module.exports = router
